package edu.harding.redzone;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class PreviewModeActivity extends Activity
{
	private final int DIALOG_NOT_IN_SDCARD_ID = 3;
	private final int DIALOG_SAVE_ID = 0;
	private final int DIALOG_DELETE_ID = 1;
	private final int DIALOG_SAVE_FIRST_ID = 2;
	private final int OFFENSE = 0;
	private final int DEFENSE = 1;
	private final int DIALOG_BACK_ID = 4;

	private Toast mToast;
	protected EditText mPlayNameSave;
	protected Context mContext;

	private final String MIME = "image/png";
	protected Play mPlay;
	protected Handler mHandler = new Handler();;
	protected Utility mUtil;
	private Uri mUri;
	private boolean mIsSaved;
	private static boolean mIsDeleted;

	private void IsSaved(boolean value)
	{
		mIsSaved = value;
	}

	private boolean IsSaved()
	{
		return mIsSaved;
	}

	public static boolean Deleted()
	{
		return mIsDeleted;
	}

	private void Deleted(boolean value)
	{
		mIsDeleted = value;
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preview);

		showToast(getString(R.string.preview_string), Toast.LENGTH_SHORT);

		mContext = this.getApplicationContext();
		mUtil = new Utility(mContext);

		IsSaved(false);
		Deleted(false);
	}
	
	@Override
	public void onBackPressed()
	{
		showDialog(DIALOG_BACK_ID);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.preview_menu, menu);
		return true;
	}

	private void showToast(String message, int duration)
	{
		mToast = Toast.makeText(getApplicationContext(), message, duration);

		mToast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
		mToast.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.send:
			if (IsSaved() == false
					&& PreviewModeView.PlayToEdit().Saved() == false)
				showDialog(DIALOG_SAVE_FIRST_ID);
			else
				onSendMenuItemClicked();
			return true;
		case R.id.save:
			showDialog(DIALOG_SAVE_ID);
			return true;
		case R.id.delete:
			if (PreviewModeView.PlayToEdit().Saved() == false)
				showDialog(DIALOG_NOT_IN_SDCARD_ID);
			else
				showDialog(DIALOG_DELETE_ID);
			return true;
		case R.id.edit:
			onEditMenuItemClicked();
			return true;
		}
		return false;
	}

	private void onEditMenuItemClicked()
	{
		mPlay = PreviewModeView.PlayToEdit();
		switch (PreviewModeView.PlayType())
		{
		case OFFENSE:
			OffenseModeView.PlayToEdit(mPlay);
			OffenseModeView.IsEdit(true);
			startActivity(new Intent(this, OffenseModeActivity.class));
			break;
		case DEFENSE:
			DefenseModeView.PlayToEdit(mPlay);
			DefenseModeView.IsEdit(true);
			startActivity(new Intent(this, DefenseModeActivity.class));
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id)
		{
		case DIALOG_BACK_ID:
			builder.setMessage(getString(R.string.back_press_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
									startActivity(new Intent(
											PreviewModeActivity.this,
											MainActivity.class));
								}
							})
					.setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									showToast(
											getString(R.string.press_menu_string),
											Toast.LENGTH_LONG);
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_SAVE_FIRST_ID:
			builder.setMessage(getString(R.string.save_first_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_NOT_IN_SDCARD_ID:
			builder.setMessage(getString(R.string.no_exist_string))
					.setCancelable(false)
					.setPositiveButton(R.string.ok,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									dialog.dismiss();
								}
							});
			dialog = builder.create();
			break;
		case DIALOG_SAVE_ID:
			Context context = getApplicationContext();
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(LAYOUT_INFLATER_SERVICE);
			final View layout = inflater.inflate(R.layout.save_dialog, null);

			builder.setView(layout);
			builder.setPositiveButton(R.string.ok,
					new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int id)
						{
							mPlayNameSave = (EditText) layout
									.findViewById(R.id.playNameSave);
							mPlay = null;
							if (PreviewModeView.PlayType() == OFFENSE)
								mPlay = OffenseModeView.PlayToSave();
							else if (PreviewModeView.PlayType() == DEFENSE)
								mPlay = DefenseModeView.PlayToSave();

							showToast(mContext.getString(R.string.saving),
									Toast.LENGTH_SHORT);

							if (mPlayNameSave != null)
								mPlay.Name(mPlayNameSave.getText().toString());
							else
								mPlay.Name("new");

							showToast(mContext.getString(R.string.saving),
									Toast.LENGTH_SHORT);

							Runnable runnable = new Runnable()
							{
								public void run()
								{
									mUtil.SavePlay(mPlay,
											PreviewModeView.Bitmap(),
											PreviewModeView.Paint(),
											PreviewModeView.Width(),
											PreviewModeView.Height());
									
									IsSaved(true);
									mPlay.Saved(true);

									mHandler.post(new Runnable()
									{
										public void run()
										{
											showToast(mContext
													.getString(R.string.saved),
													Toast.LENGTH_SHORT);
										}
									});
								}
							};
							new Thread(runnable).start();
						}
					});
			builder.setNegativeButton(R.string.cancel, null);
			dialog = builder.create();
			break;
		case DIALOG_DELETE_ID:
			builder.setMessage(R.string.delete_question)
					.setCancelable(false)
					.setPositiveButton(R.string.yes,
							new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,
										int id)
								{
									mPlay = PreviewModeView.PlayToDelete();
									showToast(mContext
											.getString(R.string.deleting),
											Toast.LENGTH_SHORT);

									Runnable runnable = new Runnable()
									{
										public void run()
										{
											mUtil.DeletePlay(mPlay.Name());
											Deleted(true);

											mHandler.post(new Runnable()
											{
												public void run()
												{
													showToast(
															mContext.getString(R.string.deleted),
															Toast.LENGTH_SHORT);
												}
											});
										}
									};
									new Thread(runnable).start();
								}
							}).setNegativeButton(R.string.no, null);
			dialog = builder.create();
			break;
		}

		return dialog;
	}

	private void onSendMenuItemClicked()
	{
		if (mUtil.EnsureSDCardAccess())
		{
			String name = PreviewModeView.PlayToSend();
			mUri = Uri.fromFile(new File(mUtil.SDCard(), name + ".png"));

			Intent intent = new Intent(android.content.Intent.ACTION_SEND);
			intent.putExtra(Intent.EXTRA_SUBJECT, name);
			intent.putExtra(Intent.EXTRA_STREAM, mUri);
			intent.setType(MIME);
			startActivity(intent);
		}
	}
}
